<?php
require_once '../include/conn.php';
require_once 'header.php';
require_once 'pager.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>访客列表</title>
    <link rel="stylesheet" href="index.css">
</head>
<body>
    <div class="table-box">
        <div class="table-item">
            <table>
                <tr>
                <td height="25" colspan="6">访客列表</td>
                </tr>
                <tr> 
                <td>访客账号</td>
                <td>状态</td>
                <td>浏览页面次数</td>
                <td>最后登录IP</td>
                <td>最后离线时间</td>
                <td>相关操作</td>
                </tr>
                <?php
                $total_count = $db->counter('h_kefu_status', 'id');
                $list_num = 30;
                $page = (int)$page;
                if($page_input){$page=$page_input;}
                $rowset = new Pager($total_count,$list_num,$page);
                $from_record = $rowset->_offset();
                $query = "select * from `h_kefu_status` order by h_addTime desc,id desc limit $from_record, $list_num";
                $result = $db->query($query);
                while($list = $db->fetch_array($result))
                {
                    $rs_list[]=$list;
                }

                $page_list = $rowset->link(GetUrl(2) . "?page=");
                
                if(count($rs_list) > 0)
                {   
                    foreach ($rs_list as $key=>$val)
                    {
                        $date_1 = date("Y-m-d H:i:s"); 
                        $date_2 = $val['h_addTime'];
                        $time=ceil(strtotime($date_1)-strtotime($date_2));
                        if($time > 3){
                            $status = '离线';
                        }else{
                            if($val['h_status'] == 1){$status = '正在会话';}else if($val['h_status'] == 2){$status = '隐藏窗口';}
                        }
                ?>     
                    <tr>
                    <td><?php if($val['h_name'] == ''){echo $val['h_user'];}else{echo $val['h_name'];} ?></td>
                    <td><?php echo $status; ?></td>
                    <td><?php echo $val['h_logins']; ?></td>
                    <td><?php echo $val['h_actIP']; ?></td>
                    <td><?php echo $val['h_addTime']; ?></td>
                    <td><a style="cursor:pointer;" onClick="javascript:hintandturn('确定要删除吗？对应聊天内容也将同时删除！','?clause=delinfo&id=<?php echo $val['id']; ?>&user=<?php echo $val['h_user']; ?>',true);">删除</a></td>
                    </tr>
                <?php
                    }
                }	
                ?>
            </table>
            <?php if(count($rs_list) > 0) echo "<div class=\"page\" style=\"margin-top:10px;\">{$page_list}</div>";?>
        </div>
    </div>

    <?php  
    if($clause == 'delinfo'){delinfo();}

    function delinfo()
    {
	global $db,$id,$user;

	$query = "delete from `h_kefu_status` where id = $id";
    $db->query($query);
    
    $sql = "select * from `h_kefu` where h_user = '" . $user . "' ";
    $query1 = $db->query($sql);
    while($rs1 = $db->fetch_array($query1)){
        $query2 = "delete from `h_kefu` where id = '{$rs1['id']}'";
        $db->query($query2);
    }
	
    echo("<script type='text/javascript'> location.href='?'; </script>");
    exit;
    }
    ?>
    <script src="/js/jquery.min.js"></script>
    <script>
        setInterval(function () {
            $(".table-box").load(location.href + " .table-box");
        }, 1500);
    
        function hintandturn(str,url,torf)
        {
            if(confirm(str) && torf){
                window.location=url;
                return true;
            }else{
                return false;
            }
        }
    </script>
</body>
</html>